#!/bin/sh

NAME="${1}"
shift
DIRECTORY="${1}"
shift
SECONDDIRECTORY="${1}"
shift

if [ "${SECONDDIRECTORY}" != "" ] ; then
	DESTDIR="${SECONDDIRECTORY}"
	REPODIR="${DESTDIR}/repo"
	FSDIRTWO="${REPODIR}/fs/${NAME}"
	mkdir -p "${FSDIRTWO}"
	FSDIR="${DESTDIR}"	
else
	DESTDIR="/"
	REPODIR="/repo"
	FSDIR="${REPODIR}/fs/${NAME}"
fi
HASHDIR="${REPODIR}/hash"

mkdir -p "${HASHDIR}"

for directory in $(find "${DIRECTORY}" -type d | sed s@${DIRECTORY}@@g)
do
	echo "${directory}"
	mkdir -p "${FSDIR}/${directory}"
done
if [ "${FSDIRTWO}" != "" ] ; then
	for directory in $(find "${DIRECTORY}" -type d | sed s@${DIRECTORY}@@g)
	do
		echo "${directory}"
		mkdir -p "${FSDIRTWO}/${directory}"
	done
fi

for file in $(find "${DIRECTORY}" -not -type d)
do
	SRCFILE="${file}"
	DESTINATIONDIR="$(echo "${file}" | sed s@${DIRECTORY}/@@g)"
	SHA256="$(sha256 -q ${file})"
	HEADER="$(echo "${SHA256}" | cut -b 1-2)"
	TRAILER="$(echo "${SHA256}" | cut -b 3-64)"
	echo "${file}"
	if [ ! -f "${HASHDIR}/${HEADER}/${TRAILER}" ] ; then
	
		mkdir -p "${HASHDIR}/${HEADER}"
		cp "${SRCFILE}" "${HASHDIR}/${HEADER}/${TRAILER}"
	fi
	if [ "${OPTIONS}" = "verbose" ] ; then
		echo "Linking ${SRCFILE} \(${SHA256}\) to ${FSDIR}"
	fi
	ln "${HASHDIR}/${HEADER}/${TRAILER}" "${FSDIR}/${DESTINATIONDIR}"
	if [ "${FSDIRTWO}" != "" ] ; then
		if [ "${OPTIONS}" = "verbose" ] ; then
			echo "Linking ${SRCFILE} \(${SHA256}\) to ${FSDIRTWO}"
		fi
		ln "${HASHDIR}/${HEADER}/${TRAILER}" "${FSDIRTWO}/${DESTINATIONDIR}"
	fi
done
